using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using MT.eVotePRS.Common;
using MT.eVotePRS.Device;
using MT.eVotePRS.Model;

namespace MT.eVotePRS.UI
{
    public partial class frmCheck : Form
    {
        StudentInfo studentInfo;
        InventoryTxn inventoryTxn;

        protected override void WndProc(ref Message m)
        {
            int devCount;

            if (m.Msg == Device.Native.Win32Message.WM_DEVICECHANGE)
            {
                switch ((int)m.WParam)
                {
                    case Device.Native.Win32Message.DBT_DEVICEARRIVAL:
                    case Device.Native.Win32Message.DBT_DEVICEREMOVECOMPLETE:

                        devCount = Program.ArsAPIHelper.ArsDeviceConnected();

                        if (0 == devCount)
                        {
                            if (Program.ArsAPIHelper.Processing)
                            {
                                Program.ArsAPIHelper.StopProcess();
                            }
                            DialogBox.boxWarning(string.Format("Receiver Count={0}, Please prepare the receiver first.", devCount.ToString()), "Warning!");
                        }
                        Thread.Sleep(10);
                        break;
                    default:
                        break;
                }
            }
            base.WndProc(ref m);
        }

        public frmCheck()
        {
            InitializeComponent();

            studentInfo = new StudentInfo();
            inventoryTxn = new InventoryTxn();
        }

        private void frmCheck_Load(object sender, EventArgs e)
        {
            // Since we are start this program, it would like to detect the device once.
            Message m = new Message();
            m.Msg = Device.Native.Win32Message.WM_DEVICECHANGE;
            m.WParam = (System.IntPtr)Device.Native.Win32Message.DBT_DEVICEARRIVAL;

            WndProc(ref m);

            int ret = Program.ArsAPIHelper.RegisterDeviceNotify((Int32)this.Handle);
            if (ret > 0)
            {
                Program.ArsAPIHelper.StartProcess();
            }

            txtHandsetID.Focus();
            txtHandsetID.SelectAll();
        }

        private void frmCheck_FormClosing(object sender, FormClosingEventArgs e)
        {
            Program.ArsAPIHelper.StopProcess();
            Program.ArsAPIHelper.UnRegisterDeviceNotify();
        }
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void txtHandsetID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                inventoryTxn.HandsetID = txtHandsetID.Text.Trim();

                if (string.IsNullOrEmpty(inventoryTxn.HandsetID) || inventoryTxn.HandsetID.Length < Constants.LEN_ID_Min)
                {
                    DialogBox.boxWarning("Invalid Handset ID!", "Warning!");
                    txtHandsetID.SelectAll();
                    return;
                }

                FindInventoryByHandset(inventoryTxn.HandsetID);
                txtHandsetID.SelectAll();
            }
        }

        delegate void _dataReceived(object sender, ARSEventArgs e);
        void dataReceiver_DataReceived(object sender, ARSEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new _dataReceived(dataReceiver_DataReceived), sender, e);
                return;
            }

            HandsetData keypadData = (HandsetData)e.ReceivedData;
            switch (keypadData.CommandCode)
            {
                case ARSCommandCode.STUDENT_JOIN:
                    FindInventoryByHandset(keypadData.MacStr);
                    break;

                default:
                    break;
            }
        }

        private void FindInventoryByHandset(string handsetId)
        {
            txtHandsetID.Text = handsetId;

            InventoryTxn inventTxn = null; //  = new InventoryTxnService_BL().GetLastTxnOfHandset(handsetId);
            if (inventTxn != null && inventTxn.Id > 0)
            {
                inventoryTxn = inventTxn;
                lblShowStatus.Text = inventoryTxn.UserEvent;
                lblShowDateTime.Text = inventoryTxn.TxnTime.ToString();

                if (! inventoryTxn.IsOverride)
                {
                    StudentInfo student = null ; // = new StudentInfoService_BL().GetStudentInfoByNo(inventoryTxn.StudentID);
                    if (student != null && student.Id > 0)
                    {
                        studentInfo = student;

                        lblShowStudentBarcode.Text = studentInfo.Barcode;
                        lblShowStudentNo.Text = studentInfo.StudentNo;
                        lblShowStudentName.Text = studentInfo.StudentName;
                        lblShowGraduate.Text = student.IsGraduate ? "Yes" : "No";

                        return;
                    }
                }
            }
            else
            {
                lblShowStatus.Text = "Nil";
                lblShowDateTime.Text = "Nil";
            }

            lblShowStudentBarcode.Text = "Nil";
            lblShowStudentNo.Text = "Nil";
            lblShowStudentName.Text = "Nil";
            lblShowGraduate.Text = "Nil";
        }
    }
}